<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

Loader::loadClass('Model');

class Session_Model_Default extends Model
{
    protected $_table = 'session';

    public function deleteSession($id) {
        return $this->delete(array('sessionid' => $id));
    }

    public function deleteExpired($expiretime) {
        return $this->delete('`lastvisit`<' . $expiretime);
    }

    public function write($id, $data) {
        if(strlen($data) > 1020) {
            $r = array();
        } else {
            $r = str_split($data, 255);
        }

        $sessiondata = array(
            'sessionid' => $id,
            'ip'        => ip(),
            'lastvisit' => SYS_TIME,
            'data1'     => $r[0],
            'data2'     => (isset($r[1]) ? $r[1] : ''),
            'data3'     => (isset($r[2]) ? $r[2] : ''),
            'data4'     => (isset($r[3]) ? $r[3] : '')
        );

        return $this->insert($sessiondata, true, true);
    }

    public function read($id) {
        $r = $this->getOne(array('sessionid' => $id), 'data1,data2,data3,data4');
        return $r ? $r['data1'].$r['data2'].$r['data3'].$r['data4'] : '';
    }
}